<template>
	<view class="service_wrap">
		<view class="info_item">
			<view class="title">基本信息</view>
			<view class="input_wrap">
				<view class="left_text">公司名称</view>
				<input class="right_text" type="text" placeholder="请输入" :value="company" @input="bindcompany" />
			</view>
			<view class="input_wrap">
				<view class="left_text">公司地址</view>
				<input class="right_text" type="text" placeholder="请输入" :value="company_city" @input="bindcity" />
			</view>
			<view class="input_wrap">
				<view class="left_text">行业</view>
				<input class="right_text" type="text" placeholder="请输入" :value="industry" @input="bindindustry" />
			</view>
		</view>
		<view class="info_item">
			<view class="title">公司介绍</view>
			<textarea placeholder="请输入" :value="intro" @input="bindintro" maxlength="2000"></textarea>
		</view>
		<view class="info_item">
			<view class="title">图片上传</view>
			<view class="add_item" @click="upImg()">
				<view class="add_img" v-if="up_img == ''">
					<image :src="remoteUrl + 'add_icon.png'" class="add_icon" mode=""></image>
					<view>点击上传</view>
				</view>
				<image class="up_img" :src="ossUrl + up_img" v-else mode=""></image>
			</view>
		</view>
		
		<view class="bot_btn">
			<view class="btn" @click="saveCompanyFun()">{{ edit ? '修改' : '保存'}}</view>
		</view>
	</view>
</template>

<script>
	import {
		remoteUrl,
		ossUrl
	} from '@/utils/config.js'
	import {uploadFile} from '@/utils/tools.js'
	import {
		saveCompany
	} from '@/api/mine.js'
	export default {
		data() {
			return {
				ossUrl: ossUrl,
				remoteUrl: remoteUrl,
				company: '',
				company_city: '',
				industry: '',
				intro: '',
				up_img: '',
				edit: false
			};
		},
		onLoad(options) {
			if (options.detail) {
				let obj = JSON.parse(options.detail)
				this.company = obj.name
				this.company_city = obj.address
				this.industry = obj.industries
				this.intro = obj.introduce
				this.up_img = obj.images
				this.edit = true
			}
		},
		onShareAppMessage(res) { //发送给朋友
			return {
				title: '小程序分享',
				imageUrl: '',
			}
		},
		methods: {
			bindcompany(e) {
				this.company = e.detail.value
			},
			bindcity(e) {
				this.company_city = e.detail.value
			},
			bindindustry(e) {
				this.industry = e.detail.value
			},
			bindintro(e) {
				this.intro = e.detail.value
			},
			upImg(e) {
				let that = this
				uni.chooseMedia({
				  count: 1,
				  mediaType: ['image'],
				  sourceType: ['album', 'camera'],
				  success(res) {
					uploadFile(res.tempFiles[0].tempFilePath).then(res => {
						console.log(res)
						if (res.code == 1) {
							that.up_img = res.data.url
						}
					})
				  }
				})
			},
			saveCompanyFun() {
				if (this.company == '') {
					uni.showToast({
						title: '请输入公司名称！',
						icon:'none'
					})
					return
				}
				if (this.company_city == '') {
					uni.showToast({
						title: '请输入公司地址！',
						icon:'none'
					})
					return
				}
				if (this.industry == '') {
					uni.showToast({
						title: '请输入行业！',
						icon:'none'
					})
					return
				}
				if (this.intro == '') {
					uni.showToast({
						title: '请输入公司介绍！',
						icon:'none'
					})
					return
				}
				if (this.up_img == '') {
					uni.showToast({
						title: '请上传图片！',
						icon:'none'
					})
					return
				}
				saveCompany({
					name: this.company,
					address: this.company_city,
					industries: this.industry,
					introduce: this.intro,
					images: this.up_img
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '保存成功！',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},600)
					}else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
.service_wrap {
	min-height: 100vh;
	background: #F5F5F5;
	padding-top: 20rpx;
	padding-bottom: 100rpx;
	.info_item {
		width: 690rpx;
		height: 420rpx;
		background: #fff;
		border-radius: 24rpx;
		margin: 30rpx auto 0;
		padding: 26rpx;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 30rpx;
		}
		.input_wrap {
			display: flex;
			padding: 30rpx 0 30rpx 0;
			border-top: 1rpx solid #00000010;
			.left_text {
				width: 200rpx;
				font-size: 28rpx;
			}
			.right_icon {
				width: 15rpx;
				height: 24rpx;
			}
			.picker {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
	
		.add_item {
			width: 180rpx;
			height: 160rpx;
			border-radius: 12rpx;
			background: #F5F5F5;
			text-align: center;
			font-size: 24rpx;
			padding-top: 20rpx;
			.add_icon {
				width: 72rpx;
				height: 72rpx;
			}
			.up_img {
				width: 100%;
				height: 100%;
				border-radius: 12rpx;
			}
		}
	}
	
	.bot_btn {
		width: 100vw;
		height: 108rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 2;
		display: flex;
		align-items: center;
	
		.btn {
			width: 650rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin: 0 auto;
			text-align: center;
			color: #fff;
			font-size: 28rpx;
			background: #1C41F1;
			border-radius: 54rpx;
		}
	}
}
</style>
